﻿Imports System.ServiceModel.DomainServices.Client
Imports LullabyFullDeCaixa.Web

Partial Public Class ConfigReportHoresTreballades
    Inherits ConfigReportAbstract

    Public Sub New()
        MyBase.New(My.Resources.StringLibrary.InformeHoresTreballades, EnumTipusReport.HoresTreballades)
        InitializeComponent()

        If Contexte.IsUsuarisLoaded Then
            OmpleDadesBuscador()
        Else
            Me.Activity.IsActive = True

            AddHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

            Contexte.LoadUsuaris()
        End If

    End Sub

#Region "Generar"

    Private Sub GenerarReport(IdUsuari As Integer, ByVal DataInici As Nullable(Of DateTime), ByVal DataFi As Nullable(Of DateTime))
        Dim uri As String = BASE_URI & "?Report=" & Me.tipusReport

        If DataInici IsNot Nothing Then
            uri &= "&DataInici=" & DataInici.Value.Ticks
        End If

        If DataFi IsNot Nothing Then
            uri &= "&DataFi=" & DataFi.Value.Ticks
        End If

        'afegim l'element filtrat
        uri &= "&IdUsuari=" & IdUsuari

        System.Windows.Browser.HtmlPage.Window.Eval("window.open('" & uri & "', '_blank')")
    End Sub

    Public Overrides Sub Generar()
        GenerarReport(Me.cboUsuaris.SelectedItem.id, Me.dtpDataInici.SelectedDate, Me.dtpDataFi.SelectedDate)
    End Sub

#End Region

#Region "Carregar Dades"
    Private Sub OmpleDadesBuscador()
        If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
            Dim LListaUsuaris As List(Of Usuari) = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
            LListaUsuaris.Insert(0, New Usuari() With {.Id = -1, .Nom = ""})
            Me.cboUsuaris.ItemsSource = LListaUsuaris
        Else
            Me.cboUsuaris.ItemsSource = {Contexte.UsuariActual}
        End If

        Me.cboUsuaris.SelectedItem = cboUsuaris.Items(0)
        Me.dtpDataInici.SelectedDate = Today.AddDays(-7).ToShortDateString
        Me.dtpDataFi.SelectedDate = Today.ToShortDateString
    End Sub

    Private Sub UsuarisCarregats(Resultat As Boolean, Missatge As String)
        RemoveHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

        If Contexte.IsUsuarisLoaded Then
            Me.Activity.IsActive = False
            OmpleDadesBuscador()
        End If
    End Sub
#End Region


End Class
